import time
from datetime import datetime

import pymysql

def get_data_by_date(input_date):#CREATE INDEX idx_rq ON t_z_data(rq);不建索引就 很慢
    """
    查询 t_z_data 表中 rq 字段(2014-06-18)等于 input_date 的记录。
    :param input_date: str, 格式如 '2025-03-27 13:51:48'
    :return: dict or None
    """
    input_date = datetime.strptime(input_date, '%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d')

    # 连接数据库
    connection = pymysql.connect(
        host='localhost',
        user='root',
        password='123456',
        database='test03',
        charset='utf8mb4',
        cursorclass=pymysql.cursors.DictCursor
    )

    try:
        with connection.cursor() as cursor:
            sql = "SELECT * FROM `t_z_data` WHERE `rq` = %s"
            cursor.execute(sql, (input_date,))
            result = cursor.fetchone()  # 只取一条记录
            if  result is not None:
                return result['kpj'],result['spj']
            return None,None
    finally:
        connection.close()
if __name__=="__main__":
    startTime=time.time()
    print(get_data_by_date("2014-06-18 13:51:48"))
    endTime=time.time()
    print(endTime-startTime)